<?php  

class Controller_Plan extends Controller_Website {

    public function action_list() {
//echo '222';exit;
        $m_order = Model::factory('plan');
        $total = $m_order->count();
        $pager = new Pager($total, 15);
        $where = array();
        $where['ORDER'] = 'create_time DESC';
//        $where['user_type'] = 0;
        $where['state'] = "1";
        $order = $m_order->select($pager->offset, $pager->size,$where)->as_array();
//        $order  = $m_order->getRow(array('state'=>1));

//        print_r($order);exit;
        foreach($order as &$o)
        {
//            print_r($o);exit;
            $user = Model::factory('personInfo');
            $user = $user->getRow(array('uid'=>$o['user_id']));
            $o['username'] = $user['real_name'];
            $project = Model::factory('project');
            $user = $project->getRow(array('id'=>$o['project_id']));
//            print_r($user);exit;
            $o['project_name'] = $user['project_name'];
            $model = Model::factory('projectProps');
            $model1 = $model->getRow(array('project_id'=>$o['project_id']));

            $o['shooting_style'] = $model1['shooting_style'];
//            if(!isset($model1)){
//                echo "没数据";exit;
//            }

//            print_r($model1);exit;
//            $model1 = $model->getRow(array('project_id'=>426));
            $dev = json_decode($model1['prop_middle2'],true);
//            print_r($dev);exit;
            $num = 0;
            foreach ($dev as $k=>$v)
            {
//                print_r($v);exit;
                if($num == $v['num']) unset($dev[$k]);  //排除掉设备中num是0的设备
            }
//            print_r($dev);exit;
            $arr = json_decode($model1['prop_middle'],true);
            foreach ($arr as $k=>$v)
            {
                if($num == $v['num']) unset($arr[$k]);  //排除掉剧组成员中num是0的成员
            }
            $u_name = "";

            foreach ($arr as $k=>$v)  //讲剧组中默认自带的成员摘出来  呈现在列表页
            {
//                print_r($v);exit;
                $username = Model::factory('personCategory');
                $username = $username->getRow(array('english_name'=>$k));
                $arr[$k] = $username['name'];
                $u_name .= $arr[$k].$v['num'].'>';
            }
//            echo $u_name;exit;
            $u_name = rtrim($u_name,'>');
//            echo $u_name;exit;
            $o['u_name'] = $u_name;
        }

//            print_r($arr);exit;
//        echo $u_name;exit;
//        print_r($order);exit;
        $this->content = View::factory('plan/list');
        $this->content->order = $order;
        $this->content->pager = $pager;
    }

    public function action_disable() {

        $id = $_GET['id'];
//        echo $id;
        $data = array(
            'producer_state' => '0',
        );
        $m_admin = Model::factory('plan');
        $ret = $m_admin->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    public function action_enable() {

        $id = $_GET['id'];
//        echo $id;exit;
        $data = array(
            'producer_state' => '1',
        );
        $m_admin = Model::factory('plan');
        $ret = $m_admin->updateById($data, $id);
        if ($ret !== false) {
            $array = [];
            $model= $m_admin->getRow(array('id'=>$id));
            $array['s_uid'] = $model['user_id'];
            $array['project_id'] = $model['project_id'];
            $project = Model::factory('project');
            $project = $project->getRow(array('id'=>$model['project_id']));
            $array['r_uid'] = $project['user_id'];
            $array['content'] = "制作人已审稿,请选导演";
            $array['add_time'] = time();
            $array['is_read'] = 0;
            $array['read_time'] = 0;
            $array['type'] = 2;
            $notice = Model::factory('notice');
            $notice = $notice->insert($array);
            $this->redirect(Request::$referrer);
        }
    }

    public function action_addPosition() {
        $id = $_GET['id'];
        $m_category = Model::factory('personCategory');
        if (!empty($_POST)) {
            $data =$_POST;
//            print_r($data);exit;
            $model = Model::factory('projectProps');
            $model1 = $model->getRow(array('project_id'=>$id));
//            $model1 = $model->getRow(array('project_id'=>426));
            $arr = json_decode($model1['prop_middle'],true);
            $category = Model::factory('personCategory');
            $category = $category->getRow(array('id'=>$data['parent_id']));
            foreach ($arr as $k=>&$a)
            {
//                print_r($a);exit;
                if($k == $category['english_name']){
                    $user_bq = [];
                    $user_bq['user_bq'] = [];
                    array_push($a['Label'],$user_bq);
//                    print_r($a);exit;
                    $a['num']++;
                }
            }
            $array = array(
                'prop_middle'=>json_encode($arr),
            );
            $ret = $model->updateById($array,$model1['id']);
            if ($ret !== false) {
                $this->redirect('plan/list');
            }
        }
        $cat_list = $m_category->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
//        print_r($cat_list) ;exit;
        $this->content = View::factory('plan/add');
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->info = null;
    }

    public function action_addEquipment() {
        $id = $_GET['id'];
        $m_category = Model::factory('deviceCategory');
        if (!empty($_POST)) {
            $data =$_POST;
            $model = Model::factory('projectProps');
            $model1 = $model->getRow(array('project_id'=>$id));
            $arr = json_decode($model1['zhizuo_prop_middle2'],true);
            $category = Model::factory('deviceCategory');
            $category = $category->getRow(array('id'=>$data['parent_id']));
            $arr[$category['english_name']] = '0.0';
            $array = array(
                'zhizuo_prop_middle2'=>json_encode($arr),
            );
            $ret = $model->updateById($array,$model1['id']);
            if ($ret !== false) {
                $this->redirect('plan/list');
            }
        }
        $cat_list = $m_category->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        $this->content = View::factory('plan/edit');
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->info = null;
    }

    public function action_updatePosition() {
        $id = $_GET['id'];
//        echo $id;exit;
        $model = Model::factory('projectProps');
        $model1 = $model->getRow(array('project_id'=>$id));
//        $model1 = $model->getRow(array('project_id'=>426));
        $arr = json_decode($model1['prop_middle'],true);
        $num = 0;
        foreach ($arr as $k=>$v)
        {
            if($num == $v['num']) unset($arr[$k]);  //排除掉剧组成员中num是0的成员
        }
//        print_r($arr);exit;
        foreach ($arr as $k=>&$a)
        {
//            print_r($k);exit;
            $pro = Model::factory('personCategory');
            $pro = $pro->getRow(array('english_name'=>$k));
//            print_r($pro);exit;
            $a['name'] = $pro['name'];
            $a['num'] =$a['num'];
        }
//        print_r($arr);exit;
//        echo $id;exit;
        $this->content = View::factory('plan/updateprs');
        $this->content->arr = $arr;
        $this->content->pid = $id;
    }

    public function action_updateDevice() {
        $id = $_GET['id'];
        $model = Model::factory('projectProps');
        $model1 = $model->getRow(array('project_id'=>$id));
        $arr = json_decode($model1['zhizuo_prop_middle2'],true);
        foreach ($arr as $k=>$a)
        {
            $pro = Model::factory('deviceCategory');
            $pro = $pro->getRow(array('english_name'=>$k));
            $arr[$k] = $pro['name'];
        }
        $this->content = View::factory('plan/updatedev');
        $this->content->arr = $arr;
        $this->content->pid = $id;
    }

    public function action_delete()
    {
        $name = $_GET['name'];
        $id   = $_GET['id'];
        $pro = Model::factory('personCategory');
        $pro = $pro->getRow(array('name'=>$name));
        $name = $pro['english_name'];
        $model = Model::factory('projectProps');
        $model1 = $model->getRow(array('project_id'=>$id));
//        print_r($model1);exit;
        $arr = json_decode($model1['zhizuo_prop_middle'],true);
        foreach ($arr as $k=>$v)
        {
            if($name == $k) unset($arr[$k]);
        }
        $array = array(
            'zhizuo_prop_middle'=>json_encode($arr),
        );
//        print_r($array);exit;
//        echo $id;exit;
        $ret = $model->updateById($array,$model1['id']);
//        var_dump($ret);exit;
        if ($ret !== false) {

            $this->redirect('plan/list');
        }
    }

    public function action_delete2()
    {
        $name = $_GET['name'];
        $id   = $_GET['id'];
        $pro = Model::factory('deviceCategory');
        $pro = $pro->getRow(array('name'=>$name));
        $name = $pro['english_name'];
        $model = Model::factory('projectProps');
        $model1 = $model->getRow(array('project_id'=>$id));
//        print_r($model1);exit;
        $arr = json_decode($model1['zhizuo_prop_middle2'],true);
        foreach ($arr as $k=>$v)
        {
            if($name == $k) unset($arr[$k]);
        }
        $array = array(
            'zhizuo_prop_middle2'=>json_encode($arr),
        );
//        print_r($array);exit;
//        echo $id;exit;
        $ret = $model->updateById($array,$model1['id']);
//        var_dump($ret);exit;
        if ($ret !== false) {
            $this->redirect('plan/list');
        }
    }

    //给所选的职位添加标签
    public function  action_add_Label()
    {
        $id   = $_GET['id'];
        $model = Model::factory('projectProps');
      $model1 = $model->getRow(array('project_id'=>$id));
//        $model1 = $model->getRow(array('project_id'=>426));
        $num1 = 0;
        $arr = json_decode($model1['prop_middle'],true);
//        print_r($arr);exit;

        if($model1['state'] == 0) {  //如果这条记录是没有加过标签的
            foreach ($arr as $k => &$v)  //根据num来讲标签字段留出来
            {
//                print_r($v);exit;
                $category = Model::factory('personCategory');
                $category = $category->getRow(array('english_name' => $k));
                $v['cid'] = $category['id'];
                $v['name'] = $category['name'];
                $Label = [];
                for ($i = 0; $i < $v['num']; $i++) {
                    $Label[$i+1] = [];
                }
//                print_r($Label);exit;
                foreach ($Label as &$l)
                {
                    $e = [];
                    $l['user_bq'] = $e;
                }

                $v['Label'] = $Label;

                $v['pid'] = $id;
            }
//        print_r($arr);exit;
            $array = array(
                'prop_middle' => json_encode($arr),
                'state'=>1
            );
            $ret = $model->updateById($array, $model1['id']);
            foreach ($arr as &$a)  //查出这个职业的分类  是否为艺人   查出中期拍摄的人员
            {
                $person_Category = Model::factory('personCategory');
                $person_Category =$person_Category->getRow(array('id'=>$a['cid']));
                $a['fid'] = $person_Category['parent_id'];
                if($a['fid'] == 99){ //如果是艺人的话
                    if(empty($a['Label'][1]['artist'])){  //如果没有artist 字段 就要循环插入一次数据
                        $num = $a['num']+1;
                        for ($i = 0; $i < $num; $i++) {  //循环创建数据放在艺人属性表中
                            $attribute = Model::factory('artistAttribute');
                            $shuju = [];
                            $shuju['type'] = 1;
                            $attribute = $attribute->insert($shuju);
//                   print_r($a);exit;
//                   echo $i;exit;
                            foreach ($a['Label'] as $k=>&$l)
                            {
                                if($k == $i){
//                           echo $i;exit;
                                    $a['Label'][$k]['artist'] = $attribute[0];
                                }
                            }
                        }
                    }
                }
            }
            $newArr = array(
                'prop_middle'=>json_encode($arr),
            );
            $ret = $model->updateById($newArr,$model1['id']);
            $model2 = $model->getRow(array('project_id' => $id));
            $arr2 = json_decode($model2['prop_middle'], true);
            $num  = 0;
            foreach ($arr2 as $k=>$v)
            {
                if($num == $v['num']) unset($arr2[$k]);  //排除掉剧组成员中num是0的成员
            }
        }else{ //这条记录是加过标签的话  就不用遍历
//            $arr['pid'] = $id;
            $arr2  = $arr;
            $num  = 0;
            foreach ($arr2 as $k=>&$v)
            {
                if($v['num'] == "0") unset($arr2[$k]); //排除掉剧组成员中num是0的成员
//                print_r($v);exit;
                foreach ($v['Label'] as $k=>&$l){
                    if(!isset($l['biaoqian'])){ //如果有bianqian这个字段就  将特征查出来
//                        echo '222';exit;
                        $bianqian = "";   //无特征
                       $v['Label'][$k]['user_bq'] = $bianqian;
                    }else{   //有特征
//                       print_r($l);exit;
                       $bianqian = explode(',',$l['biaoqian']);
                       $e = array_pop($bianqian);
                       $biaoqian = "";
                       foreach ($bianqian as $b){
                           $bq = Model::factory('feature');
                           $bq = $bq->getRow(array('id'=>$b));
//                           print_r($bq);exit;
                           $biaoqian .= $bq['tag']."-";
                       }
                       $biaoqian = rtrim($biaoqian,"--");
                        $v['Label'][$k]['user_bq'] = $biaoqian;

                    }
                }
            }
//            print_r($arr2);exit;
            foreach ($arr2 as &$new)
            {
                if($new['fid'] == 99){
                    foreach ($new['Label'] as $k=>&$l)
                    {
//                        print_r($k);exit;
                        $artist = Model::factory('artistAttribute');
                        $artist = $artist->getRow(array('id'=>$l['artist']));
                        $new['Label'][$k]['sex'] = $artist['sex'];
                        $new['Label'][$k]['age'] = $artist['age'];
                        $new['Label'][$k]['nationality'] = $artist['nationality'];
                    }
                }
            }
//            print_r($arr2);exit;
        }
//        print_r($arr2);exit;

        array_multisort(array_column($arr2,'fid'),SORT_ASC,$arr2); //根据fid来排序 越大的越后  SORT_DESC 是倒叙
        $this->content = View::factory('plan/add_Label');
        $this->content->arr = $arr2;
    }

    //添加动画制作
    public function action_add_animation($id)
    {


    }

    //点击减少人数
    public function action_increase()
    {
        $name = $_POST['name'];
        $pid  = $_POST['pid'];
//        $id  = 426;
        $model = Model::factory('projectProps');
        $model1 = $model->getRow(array('project_id'=>$pid));
        $arr = json_decode($model1['prop_middle'],true);


        $category = Model::factory('personCategory');
        $category = $category->getRow(array('name'=>$name));
//        print_r($model1);exit;
        foreach ($arr as $k=>&$a)
        {
            if($k == $category['english_name']){
                array_pop($a['Label']);
                $num = $a['num']--;

            }
        }
        $array = array(
            'prop_middle'=>json_encode($arr),
        );
        $ret = $model->updateById($array,$model1['id']);
       echo $num-1;
    }

    //点击添加人数
    public function action_reduce()
    {
        $name = $_POST['name'];
        $pid  = $_POST['pid'];
//        $id  = 426;
        $model = Model::factory('projectProps');
        $model1 = $model->getRow(array('project_id'=>$pid));
        $arr = json_decode($model1['prop_middle'],true);
        $category = Model::factory('personCategory');
        $category = $category->getRow(array('name'=>$name));
//        print_r($arr);exit;
        foreach ($arr as $k=>&$a)
        {
            if($k == $category['english_name']){
                $user_bq = [];
                $user_bq['user_bq'] = [];
                array_push($a['Label'],$user_bq);
//                print_r($a['Label']);exit;
                $num = $a['num']++;
//                print_r($a);exit;
            }
        }
//        print_r($arr);exit;
        $array = array(
            'prop_middle'=>json_encode($arr),
        );
        $ret = $model->updateById($array,$model1['id']);
        echo $num+1;
    }

    //点击获取人才对应的特征
    public function action_select()
    {
        $cid = $_POST['cid'];
        $model = Model::factory('feature');
        $model = $model->getAll(array('person_cid'=>$cid))->as_array();
//        print_r($model);
        echo json_encode($model);
    }

    //点击添加人才对应的特征
    public function action_insert()
    {
        $cid = $_POST['cid'];
        $zhiye_id = $_POST['zhiye_id'];
        $xiabiao  = $_POST['xiabiao'];
        $id  = $_POST['pid'];
//        echo $id;exit;
        $model = Model::factory('projectProps');
      $model1 = $model->getRow(array('project_id'=>$id));
//        $model1 = $model->getRow(array('project_id'=>426));
        $num = 0;
        $arr = json_decode($model1['prop_middle'],true);
//        print_r($arr);exit;
        foreach ($arr as &$v)
        {
//            print_r($v);exit;
            if($v['cid']== $zhiye_id){
                foreach ($v['Label'] as $k=>&$vv){
//                    print_r($vv);exit;
                    if($k == $xiabiao){
//                        print_r($vv);exit;
                        if(!isset($vv['biaoqian'])){  //如果不存在标签
//                            echo '3333';exit;
                            $biaoqian = "";
//                            $biaoqian = $vv['biaoqian'];
                        }else{
//                            echo '444';exit;
                            $biaoqian = $vv['biaoqian'];
                        }
                        $biaoqian .=$cid.',';

//                        $biaoqian = rtrim($biaoqian,',');
//                        print_r($biaoqian);exit;
                        $vv['biaoqian'] = $biaoqian;
                        $shuzu = explode(',',$vv['biaoqian']);
                        $shuzu =array_unique($shuzu);
                        $vv['biaoqian'] = implode(',',$shuzu);
//                        echo $vv['biaoqian'];exit;
                    }
                }
            }
        }
//        print_r($arr);exit;
        $array = array(
            'prop_middle'=>json_encode($arr),
        );
//        var_dump($array);exit;
        $ret = $model->updateById($array,$model1['id']);
        if($ret){
            echo json_encode(array('errno'=>1, 'errmsg'=>'请求成功'));
            exit;
        }else{
            echo json_encode(array('errno'=>100, 'errmsg'=>'请求失败'));
            exit;
        }


//        echo '222';

    }

    //重置标签
    public function action_Reset()
    {
        $data = $_GET;
        $xiabiao = $data['id'];
        $zhiye_id = $data['zhiye_id'];
        $id = $data['id'];
//        print_r($data);exit;
        $model = Model::factory('projectProps');
      $model1 = $model->getRow(array('project_id'=>$id));
//        $model1 = $model->getRow(array('project_id'=>426));
        $arr = json_decode($model1['prop_middle'],true);
        foreach ($arr as &$v)
        {
//            print_r($v);exit;
            if($v['cid']== $zhiye_id){  //如果里面的职业相等
                foreach ($v['Label'] as $k=>&$vv){
//                    print_r($vv);exit;
                    if($k == $xiabiao){ //在匹配到下标相等
//                        print_r($vv);exit;
                      unset($vv['biaoqian']);
                    }
                }
            }
        }
//        print_r($arr);exit;
        $array = array(
            'prop_middle'=>json_encode($arr),
        );
//        var_dump($array);exit;
        $ret = $model->updateById($array,$model1['id']);
        if ($ret !== false) {
            echo "重置成功!";
        }
    }

    //添加艺人性别属性
    public function action_sex()
    {
        $data = $_POST;
//        print_r($data);exit;
        $model = Model::factory('projectProps');
        $model1 = $model->getRow(array('project_id'=>$data['pid']));
        $arr = json_decode($model1['prop_middle'],true);
        foreach ($arr as $a){
            if($a['cid'] == $data['cid']){
                foreach ($a['Label'] as $k=>&$l){
                    if($k == $data['xiabiao']){
                        echo $l['artist'];
                        $sex = Model::factory('artistAttribute');
                        $where = array('sex'=>$data['val']);
                        $ret = $sex->updateById($where,$l['artist']);
                        if($ret){
                            echo json_encode(array('errno'=>1, 'errmsg'=>'请求成功'));
                            exit;
                        }else{
                            echo json_encode(array('errno'=>100, 'errmsg'=>'请求失败'));
                            exit;
                        }
                    }
                }
            }
        }

    }

    //添加艺人年龄属性
    public function action_age()
    {
        $data = $_POST;
//        print_r($data);exit;
        $model = Model::factory('projectProps');
        $model1 = $model->getRow(array('project_id'=>$data['pid']));
        $arr = json_decode($model1['prop_middle'],true);
        foreach ($arr as $a){
            if($a['cid'] == $data['cid']){
                foreach ($a['Label'] as $k=>&$l){
                    if($k == $data['xiabiao']){
                        echo $l['artist'];
                        $sex = Model::factory('artistAttribute');
                        $where = array('age'=>$data['val']);
                        $ret = $sex->updateById($where,$l['artist']);
                        if($ret){
                            echo json_encode(array('errno'=>1, 'errmsg'=>'请求成功'));
                            exit;
                        }else{
                            echo json_encode(array('errno'=>100, 'errmsg'=>'请求失败'));
                            exit;
                        }
                    }
                }
            }
        }
    }

    //添加艺人国籍属性
    public function action_nationality()
    {
        $data = $_POST;
//        print_r($data);exit;
        $model = Model::factory('projectProps');
        $model1 = $model->getRow(array('project_id'=>$data['pid']));
        $arr = json_decode($model1['prop_middle'],true);
        foreach ($arr as $a){
            if($a['cid'] == $data['cid']){
                foreach ($a['Label'] as $k=>&$l){
                    if($k == $data['xiabiao']){
                        echo $l['artist'];
                        $sex = Model::factory('artistAttribute');
                        $where = array('nationality'=>$data['val']);
                        $ret = $sex->updateById($where,$l['artist']);
                        if($ret){
                            echo json_encode(array('errno'=>1, 'errmsg'=>'请求成功'));
                            exit;
                        }else{
                            echo json_encode(array('errno'=>100, 'errmsg'=>'请求失败'));
                            exit;
                        }
                    }
                }
            }
        }
    }



    //选择制作风格
    public function action_Shooting()
    {
        $data = $_POST;
        $model = Model::factory('projectProps');
        $model1 = $model->getRow(array('project_id'=>$data['pid']));
//        echo '222';exit;
        $where = array('shooting_style'=>$data['val']);
        $ret = $model->updateById($where,$model1['id']);
        if($ret){
            echo json_encode(array('errno'=>1, 'errmsg'=>'请求成功'));
            exit;
        }else{
            echo json_encode(array('errno'=>100, 'errmsg'=>'请求失败'));
            exit;
        }
    }
}
